/**
* @file interface_spi.h
* @author Clemens Zangl
* @date 22.05.2012
* @version 0.1
*
* (c) 2012 University of Applied Sciences Karlsruhe
*
* File to implement a class for the SPI hardware interface
** 
*/

#ifndef __INTERFACE_SPI_H
#define	__INTERFACE_SPI_H

#include <stdio.h>
#include "stm32f4xx.h"


using namespace std;

/** 
Namespace used for all different hardware interfaces to bus systems and protocolls
*/
namespace HardwareInterface
{
	
	/**
	Configuration whether the SPI interface is a master or a slave
	*/
	typedef enum
	{
		configurationSPIMaster,
		configurationSPISlave
	}spiConfiguration;

	/**
	Defines which of the SPI interfaces of the microprocessor is used
	*/
	typedef enum
	{
		deviceSPIOne,
		deviceSPITwo
	}spiDevice;

	/**
	Class to allow easy access to the two SPI interaces on the ARM32F4.
	*/
	class SPIInterface
	{
	public:
		/**
		Constructor.
		\param [in] device Choose the SPI device on the ARM32F4 (SPI1 or SPI2).
		\param [in] configuration Define whether the SPI bus is started in a master or slave configuration. 
		*/
		SPIInterface(spiDevice device, spiConfiguration configuration);	

		/**
		Destructor.
		*/
		~SPIInterface(void);
		
		/**
		Sends a byte over the SPI bus.
		\param [in] byte The byte to send.
		\return Always 0.
		*/
		int sendByte(unsigned char byte);

		/**
		Sends a byte array over the SPI bus.
		\param [in] buffer The buffer to send.
		\param [in] bufferSize The size of the buffer to send.
		\return Always 0.
		*/
		int send(const unsigned char *buffer, size_t bufferSize);

		/**
		Reads a byte from the SPI bus.
		\param [out] result The read byte from the bus.
		\return Always 0.
		*/
		int readByte(unsigned char &result);

		/**
		Returns a status flag from the SPI bus.
		\param [in] flag The status flag to read.
		\param [out] status The read status flag.
		\return Always 0.
		*/
		int getFlagStatus(unsigned short flag, FlagStatus &status) const;
		
		/**
		Returns the SPI device used in this class instance.
		*/
		spiDevice getSPIDevice(void) const;

		/**
		Returns the configuration (Master or Slave) of the SPI bus used in this class instance.
		*/
		spiConfiguration getSPIConfiguration(void) const;
		
	private:
		/**
		Returns the internal SPI device address.
		*/
		SPI_TypeDef* getSPIDeviceID(void) const;

		/// which of the uC SPI devices is used
		spiDevice _device;

		/// Defines the configuration of this SPI bus whether it is i a master or slave configuration.
		spiConfiguration _configuration;
	};
}

#endif
